﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Channels;
using System.Configuration;


namespace Blank.wcf.ClientConnectLib
{
    public class ClientChannel<TContract>
    {
        public TContract ClientContract { get; private set; }
        private static ClientChannel<TContract> instance;
        private static object _lock = new object();
        /// <summary>
        /// 从配置文件中取
        /// </summary>
        /// <returns></returns>
        public static ClientChannel<TContract> GetInstance()
        {
            if (instance == null)
            {
                lock (_lock)
                {
                    if (null == instance)
                    {
                        try
                        {
                            instance = new ClientChannel<TContract>(ServiceProxyFactory.Create<TContract>("IMainContract"));
                            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                            if (config != null)
                            {
                                ChannelFactoryCreator.Address = config.AppSettings.Settings["Address"].Value;
                                ChannelFactoryCreator.Invoke();
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new ArgumentNullException("Configuration");
                        }
                    }
                }
            }
            return instance;
        }
        private ClientChannel(TContract iContract)
        {
            this.ClientContract = iContract;
        }
    }
}
